package com.coder4j.javafx;

import javafx.event.EventHandler;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Rectangle;

public class MyImageViewFactory extends ImageView {
    private ImageView imageView;
    private Image image;
    public MyImageViewFactory init(String url) {
        imageView = new ImageView(ComponentUtils.getImage(url));
        imageView.setPreserveRatio(true);
        return this;
    }

    public MyImageViewFactory fitWidth(int width) {
        imageView.setFitWidth(width);
        return this;
    }
    public MyImageViewFactory fitHeight(int height){
        imageView.setFitHeight(height);
        return this;
    }

    public MyImageViewFactory radius(int width,int height,int radiusNumber){
        // 创建一个Rectangle作为Clip
        Rectangle clip = new Rectangle(width,height);
        clip.setArcWidth(radiusNumber);
        clip.setArcHeight(radiusNumber);

        // 将Clip应用于ImageView
        imageView.setClip(clip);
        return this;
    }

    /**
     * 鼠标变小手
     * @return
     */
    public MyImageViewFactory cursorPoint(){
        // 添加鼠标进入事件监听器
        imageView.addEventHandler(MouseEvent.MOUSE_ENTERED, event -> {
            // 当鼠标进入ImageView时，改变鼠标形状为小手
            imageView.setCursor(javafx.scene.Cursor.HAND);
        });

        // 添加鼠标离开事件监听器
        imageView.addEventHandler(MouseEvent.MOUSE_EXITED, event -> {
            // 当鼠标离开ImageView时，恢复默认鼠标形状
            imageView.setCursor(javafx.scene.Cursor.DEFAULT);
        });
        return this;
    }

    /**
     * 添加监听
     * @param eventHandler
     * @return
     */
    public MyImageViewFactory addClickListener(final EventHandler<MouseEvent> eventHandler) {
        cursorPoint();
        imageView.addEventHandler(MouseEvent.MOUSE_CLICKED,eventHandler);

        return this;
    }

    public ImageView build() {
        return imageView;
    }

}
